package cn.com.nes.site.modules.opportuntiy;

import cn.com.nes.mybatis.agent.crm.entity.Opportunity;
import cn.com.nes.mybatis.agent.project.entity.ProjectCustomerRelationship;
import cn.com.nes.mybatis.agent.project.entity.TProject;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.entity.em.crm.customer.EnergyTypeEnum;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityClassificationEnum;
import cn.com.nes.site.entity.em.project.ProjectRelationTypeEnum;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityInfoDto;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 *
 */
@Slf4j
@Service
public class RetailOpportunityHandle extends CommonOpportunityHandle {

    public RetailOpportunityHandle() {
        log.debug("RetailOpportunityHandle instance ....");
    }

    /**
     * 商务项目类型
     * @return
     */
    @Override
    public OpportunityClassificationEnum opportunityClassification() {
        return OpportunityClassificationEnum.RETAIL;
    }

    @Override
    protected void settingDefaultFields(OpportunityBO opportunityBO, Opportunity opportunity) {
        super.settingDefaultFields(opportunityBO, opportunity);

        log.debug("RetailOpportunityHandle settingDefaultFields ....");
        opportunity.setXzqhdm(opportunityBO.getAreaCode());
        opportunity.setProjectArea(opportunityBO.getProjectArea());
        opportunity.setHouseNumber(opportunityBO.getHouseNumber());
        opportunity.setInstallPhotovoltaic(opportunityBO.getInstallPhotovoltaic());
        opportunity.setCategoryOfDeclaration(ObjectUtil.isNotNull(opportunityBO.getCategoryOfDeclaration()) ? opportunityBO.getCategoryOfDeclaration()
                .getKey() : null);
        opportunity.setNewlyDeclaredArea(opportunityBO.getNewlyDeclaredArea());
        opportunity.setOriginalDeclaredArea(opportunityBO.getOriginalDeclaredArea());
        if (EnergyTypeEnum._0.equals(opportunityBO.getEnergyType())) {
            opportunity.setElectricityMeterNumbers(opportunityBO.getMeterNumber());
        }
        if (EnergyTypeEnum._1.equals(opportunityBO.getEnergyType())) {
            opportunity.setGasMeterNumbers(opportunityBO.getMeterNumber());
        }
        opportunity.setHouseholdRegistration(opportunityBO.getHouseholdRegistration());
        opportunity.setBuildNature(opportunityBO.getBuildNature());
        opportunity.setBulidType(opportunityBO.getBulidType());
        opportunity.setPersonalPayment(opportunityBO.getPersonalPayment());

        settingHouseFields(opportunityBO, opportunity);
    }

    @Override
    protected void postProcessorForRelationshipHandle(Opportunity opportunity, OpportunityBO opportunityBO, TProject project) throws Exception {
        super.postProcessorForRelationshipHandle(opportunity, opportunityBO, project);
        log.debug("RetailOpportunityHandle postProcessorForRelationshipHandle ....");

        // 添加从关系
        ProjectCustomerRelationship relationship = projectCustomerRelationshipService.getOtherRelationship(opportunity.getCustomerId(), opportunity.getUuid());
        if (ObjectUtil.isNull(relationship)) {
            relationship = new ProjectCustomerRelationship();
            relationship.setMainFlag(ProjectRelationTypeEnum.OTHER.getKey());
            relationship.setDataType(opportunityBO.getProjectRelationDataTypeEnum().getKey());
            relationship.setImportBatch(opportunityBO.getOperateTimeStamp());
            relationship.setDeleted(DeleteEnum._0.getKey());
            relationship.setGmtCreateId(opportunityBO.getOperateUserId());
            relationship.setGmtCreate(opportunityBO.getOperateDate());
        } else {
            relationship.setGmtModified(opportunityBO.getOperateDate());
            relationship.setGmtModifiedId(opportunityBO.getOperateUserId());
        }
        relationship.setProjectUuid(ObjectUtil.isNull(project) ? null : project.getUuid());
        relationship.setMainCustomerId(opportunity.getCustomerId());
        relationship.setMainBusinessId(opportunity.getUuid());
        relationship.setCustomerId(opportunity.getCustomerId());
        relationship.setBusinessId(opportunity.getUuid());
        projectCustomerRelationshipService.saveOrUpdate(relationship);

        if (StrUtil.isEmpty(opportunityBO.getRelateOpportunityUuid())) {
            return;
        }

        // 向政府公共项目中添加从关系
        ProjectCustomerRelationship mainRelationship = projectCustomerRelationshipService.getMainRelationshipByOpportunityUuid(opportunityBO.getRelateOpportunityUuid());
        ProjectCustomerRelationship projectCustomerRelationship = projectCustomerRelationshipService.getOtherRelationship(opportunity.getCustomerId(), mainRelationship.getMainBusinessId());
        if (ObjectUtil.isNull(projectCustomerRelationship)) {
            projectCustomerRelationship = new ProjectCustomerRelationship();
            projectCustomerRelationship.setDeleted(DeleteEnum._0.getKey());
            projectCustomerRelationship.setGmtCreateId(opportunityBO.getOperateUserId());
            projectCustomerRelationship.setGmtCreate(opportunityBO.getOperateDate());
            projectCustomerRelationship.setMainFlag(ProjectRelationTypeEnum.OTHER.getKey());
            projectCustomerRelationship.setImportBatch(opportunityBO.getOperateTimeStamp());
            projectCustomerRelationship.setDataType(opportunityBO.getProjectRelationDataTypeEnum().getKey());
        } else {
            projectCustomerRelationship.setGmtModifiedId(opportunityBO.getOperateUserId());
            projectCustomerRelationship.setGmtModified(opportunityBO.getOperateDate());
        }
        projectCustomerRelationship.setProjectUuid(mainRelationship.getProjectUuid());
        projectCustomerRelationship.setMainCustomerId(mainRelationship.getMainCustomerId());
        projectCustomerRelationship.setMainBusinessId(mainRelationship.getMainBusinessId());
        projectCustomerRelationship.setBusinessId(opportunity.getUuid());
        projectCustomerRelationship.setCustomerId(opportunity.getCustomerId());
        projectCustomerRelationshipService.saveOrUpdate(projectCustomerRelationship);
    }

    /**
     * Sets various fields of an Opportunity object based on the corresponding values from an OpportunityBO object.
     * This method is intended to populate an Opportunity entity with detailed house-related information
     * extracted from a business object (OpportunityBO), facilitating data transfer between layers or modules.
     * @param opportunityBO The source business object containing house details to be applied.
     *                      Must be a valid instance with relevant fields set.
     * @param opportunity   The target entity whose house-related fields are to be updated.
     *                      Must be a mutable Opportunity instance.
     */
    private void settingHouseFields(OpportunityBO opportunityBO, Opportunity opportunity) {
        opportunity.setHouseType(opportunityBO.getHouseType());
        opportunity.setHeatingArea(opportunityBO.getHeatingArea());
        opportunity.setCommonHeatingArea(opportunityBO.getCommonHeatingArea());
        opportunity.setBuildingHeight(ObjectUtil.isNotNull(opportunityBO.getBuildingHeight()) ? opportunityBO.getBuildingHeight() : null);
        opportunity.setBuildingStructure(opportunityBO.getBuildingStructure());
        opportunity.setOuterWallThickness(opportunityBO.getOuterWallThickness());
        opportunity.setOuterWallKeepWarm(opportunityBO.getOuterWallKeepWarm());
        opportunity.setHeatingMode(opportunityBO.getHeatingMode());
        opportunity.setInstallPosition(opportunityBO.getInstallPosition());
        opportunity.setHousePerpose(opportunityBO.getHousePerpose());
        opportunity.setRoomCount(opportunityBO.getRoomCount());
        opportunity.setDrawingNo(opportunityBO.getDrawingNo());
        opportunity.setDesignCompany(opportunityBO.getDesignCompany());
        opportunity.setWindowType(opportunityBO.getWindowType());
        opportunity.setDesignHeatingLoad(opportunityBO.getDesignHeatingLoad());
        opportunity.setEndState(opportunityBO.getEndState());
        opportunity.setHouseDirection(opportunityBO.getHouseDirection());
        opportunity.setHeatingEnd(opportunityBO.getHeatingEnd());
        opportunity.setIsVillage(opportunityBO.getIsVillage());
        opportunity.setRenovationType(ObjectUtil.isNull(opportunityBO.getRenovationType()) ? null : opportunityBO.getRenovationType()
                .getKey());
    }

    @Override
    protected void handleDefaultFieldsForOpportunityBaseInfo(OpportunityInfoDto opportunityInfoDto) throws Exception {
        super.handleDefaultFieldsForOpportunityBaseInfo(opportunityInfoDto);

        log.debug("RetailOpportunityHandle handleDefaultFieldsForOpportunityBaseInfo ....");
        // 能源分类名称处理
        if (StrUtil.isNotEmpty(opportunityInfoDto.getEnergyType())) {
            opportunityInfoDto.setEnergyTypeName(EnergyTypeEnum.enumConvertKeyToValue(opportunityInfoDto.getEnergyType()));
            if (EnergyTypeEnum._0.getKey().equals(opportunityInfoDto.getEnergyType())) {
                opportunityInfoDto.setMeterNumber(opportunityInfoDto.getElectricityMeterNumbers());
            }
            if (EnergyTypeEnum._1.getKey().equals(opportunityInfoDto.getEnergyType())) {
                opportunityInfoDto.setMeterNumber(opportunityInfoDto.getGasMeterNumbers());
            }
        }
    }

}
